function serialize(a) {
	var s = [];
	if( a.constructor == Array ) {
		for (var i=0; i< a.length ; i++ )
			s.push( a[i].name + "=" + encodeURIComponent(a[i].value));
	} else {
		for (var i in a)
			s.push( i + "=" + encodeURIComponent(a[i]));
	}
	return s.join('&');
}

function getXmlHttpRequest() {
	var req = null;
	try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
		try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {
			try { req = new XMLHttpRequest(); } catch(e) {}
		}
	}
	if (req == null) throw new Error('XMLHttpRequest not supported');
	return req;
}

function ajax( options ) {
    options = {
        'type' : (options.type && options.type.toUpperCase()) || 'GET',
        'url' : options.url || '',
		'dataType' : options.dataType || '',
        'timeout' : options.timeout || 5000,
        'onComplete' : options.onComplete || function(){},
        'onError' : options.onError || function(){},
        'onSuccess' : options.onSuccess || function(){},
        'data' : options.data ? serialize(options.data) : null
    };
	if (options.data && options.type == "GET"){
		options.url = options.url.indexOf('?') > -1 ?
			options.url + "&" + options.data : options.url + '?' + options.data;
		options.data = null;
	}

    var xml = getXmlHttpRequest();
    xml.open(options.type, options.url, true);
    var timeoutLength = 5000;
    var requestDone = false;
    setTimeout(function(){ requestDone = true; }, timeoutLength);
    xml.onreadystatechange = function(){
        if ( xml.readyState == 4 && !requestDone ) {
            if ( httpSuccess( xml ) )
                options.onSuccess( httpData( xml, options.dataType ) );
            else
                options.onError();

            options.onComplete();
            xml = null;
        }
    };
	if (options.type == "POST"){
		xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if (xml.overrideMimeType){
			xml.setRequestHeader("connection", "close");
		}
	}
    xml.send(options.data);

    function httpSuccess(r) {
        try {
            return !r.status && location.protocol == "file:" ||
                ( r.status >= 200 && r.status < 300 ) ||
                r.status == 304 ||
                navigator.userAgent.indexOf("Safari") >= 0 && typeof r.status == "undefined";
        } catch(e){}
        return false;
    }

    function httpData(r,type) {
        var ct = r.getResponseHeader("content-type");
        var isxml = !type && ct && ct.indexOf("xml") >= 0;
        data = (type == "xml") || isxml ? r.responseXML : r.responseText;
        if ( type == "script" ){
        	runScript(data);
        }
        return data;
    }
}

function require(url, xml) {
	var req = getXmlHttpRequest();
	req.open("GET", url, false);
	req.send(null);
	return xml ? req.responseXML : req.responseText;
}

function runScript(code){
	var elem = create("script");
	elem.type = "text/javascript";
	if( isIE() ){// IE
		elem.text = code;
	} else {
		append(elem, (document.createTextNode(code)));
	}
	query("head").append(elem);
}

function runCss(code) {
	if( isIE() ){// IE
		var el = document.createStyleSheet();
		el.cssText = code;
	} else {
		var el = document.createElement("style");
		el.type = "text/css";
		el.appendChild(document.createTextNode(code));
		document.getElementsByTagName("head")[0].appendChild(el);
	}
}

function include(src, callback) {
	var el = document.createElement("script");
	el.type = "text/javascript";
	el.src = src;
	el.language = "Javascript";
	document.getElementsByTagName("head")[0].appendChild(el);
	if (typeof callback == "function"){ // 有回调函数
		if ( isIE() ){ // IE
			el.onreadystatechange = function(){
				var state = el.readyState;
				if (state == "loaded" || state == "interactive" || state == "complete")
					callback();
			};
		} else {
			el.onload = function(){
				callback();
			};
		}
	}
}

function link(css) {
	var el = document.createElement("link");
	el.type = "text/css";
	el.rel = "stylesheet";
	el.href = css;
	document.getElementsByTagName("head")[0].appendChild(el);
}

function ajaxform(form, fn) {
	if(typeof ajaxform.callback == "undefined") {
		ajaxform.callback = function(){
			ajaxform.fn( html(ajaxform.iframe.contentWindow.document.body) );
		}
	}
	form = form.constructor == String ? id(form) : form;
	var iframe = id('ajaxframe');
	if (! iframe){
		iframe = create("<iframe id='ajaxframe' name='ajaxframe'></iframe>");
		hide(iframe);
		append(document.body, iframe);
	}

	ajaxform.fn = fn || function(){};
	ajaxform.iframe = iframe;

	attr(form, "target", 'ajaxframe');
	if(iframe.attachEvent) {
		iframe.detachEvent ('onload', ajaxform.callback);
		iframe.attachEvent('onload', ajaxform.callback);
	} else {
		iframe.removeEventListener('load', ajaxform.callback, true);
		iframe.addEventListener('load', ajaxform.callback, false);
	}
	form.submit();
}